package com.example.yiqing;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.widget.Button;
import android.widget.TextView;

import androidx.navigation.fragment.NavHostFragment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Goble extends AppCompatActivity {
    Function function = null;
    final int[] i = {10};
    TextView text_refreshTime = null;
    TextView text_CurrentConfirmedCount = null;
    TextView text_ConfirmedCount = null;
    TextView text_CuredCount = null;
    TextView text_DeadCount = null;
    String CurrentConfirmedCount,ConfirmedCount,CuredCount,DeadCount= null;
    Button button_go_goble = null;
    Handler handler  = null;
    Handler handler1 = null;
    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.globe_yiqing);
        function = new Function();
        text_CurrentConfirmedCount = findViewById(R.id.text_CurrentConfirmedCount);
        text_ConfirmedCount = findViewById(R.id.text_ConfirmedCount);
        text_CuredCount = findViewById(R.id.text_CuredCount);
        text_DeadCount = findViewById(R.id.text_DeadCount);
        button_go_goble = findViewById(R.id.button_GoChinaYiQing);
        button_go_goble.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Goble.this,China.class);
                startActivity(intent);
                finish();
            }
        });
        autoRefresh();
        handler1 = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 0:
                        text_CurrentConfirmedCount.setText(CurrentConfirmedCount);
                        text_CuredCount.setText(CuredCount);
                        text_ConfirmedCount.setText(ConfirmedCount);
                        text_DeadCount.setText(DeadCount);
                        break;
                }
            }
        };
        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 0:
                        text_refreshTime.setText("数据将在 "+ i[0] +" 秒后刷新");
                        break;
                    case 1:
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                JSONObject jsonObject = function.getGoble();
                                try{
                                    CurrentConfirmedCount = jsonObject.getString("currentConfirmedCount");
                                    ConfirmedCount =jsonObject.getString("confirmedCount");
                                    CuredCount = jsonObject.getString("curedCount");
                                    DeadCount= jsonObject.getString("deadCount");
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                                handler1.sendEmptyMessage(0);
                            }
                        }).start();
                }
            }
        };
    }

    void autoRefresh(){
        text_refreshTime = findViewById(R.id.textView_refreshTime);
        //每十秒自动刷新数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while(true){
                        if (i[0] == 10){
                            handler.sendEmptyMessage(1);

                        }
                        Thread.sleep(1000);
                        handler.sendEmptyMessage(0);
                        i[0]--;
                        if (i[0] == 0){
                            i[0] = 10;
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }




}

